package com.sanhe.roller.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.plata.base.utils.WeakHandler;
import com.sanhe.roller.R;
import com.sanhe.roller.databinding.PointViewBinding;
import com.sanhe.roller.it.PointClickListener;

public class PointView extends FrameLayout {

    PointViewBinding binding;
    PointClickListener listener;
    WeakHandler h = new WeakHandler();

    public PointView(@NonNull Context context) {
        this(context, null);
    }

    public PointView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PointView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public PointView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(attrs, defStyleAttr, defStyleRes);
    }

    private void init(AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        binding = PointViewBinding.inflate(LayoutInflater.from(getContext()), this);

        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.PointView, defStyleAttr, defStyleRes);

        final Drawable d = a.getDrawable(R.styleable.PointView_pointSrc);
        if (d != null) {
            binding.normal.setImageDrawable(d);
        }
        a.recycle();

        binding.normal.setOnClickListener(v -> {
            boolean bo = !v.isSelected();
            v.setSelected(bo);
            if (bo) {
                binding.save.setVisibility(View.VISIBLE);
                binding.reset.setVisibility(View.VISIBLE);
            } else {
                binding.save.setVisibility(View.INVISIBLE);
                binding.reset.setVisibility(View.INVISIBLE);
            }
            if (listener != null) {
                listener.onClick(v, 0);
            }
        });
        binding.save.setOnClickListener(v -> {
            v.setVisibility(View.INVISIBLE);
            binding.normal.setEnabled(false);
            binding.reset.setVisibility(View.INVISIBLE);
            binding.anim.setVisibility(View.VISIBLE);
            binding.anim.playAnimation();
            h.postDelayed(() -> {
                binding.normal.setEnabled(true);
                binding.normal.setSelected(false);
                binding.anim.setVisibility(View.INVISIBLE);
            }, binding.anim.getDuration());

            if (listener != null) {
                listener.onClick(v, 1);
            }
        });
        binding.reset.setOnClickListener(v -> {
            v.setVisibility(View.INVISIBLE);
            binding.normal.setEnabled(false);
            binding.save.setVisibility(View.INVISIBLE);
            binding.anim.setVisibility(View.VISIBLE);
            binding.anim.playAnimation();
            h.postDelayed(() -> {
                binding.normal.setEnabled(true);
                binding.normal.setSelected(false);
                binding.anim.setVisibility(View.INVISIBLE);
            }, binding.anim.getDuration());

            if (listener != null) {
                listener.onClick(v, 2);
            }
        });
    }

    public void setonPointClickListener(PointClickListener listener) {
        this.listener = listener;
    }
}
